<?
error_reporting(E_ALL);
require_once(dirname(__FILE__) . '/../classes/config.inc.php');
require_once(dirname(__FILE__) . '/../classes/database.inc.php');
require_once(dirname(__FILE__) . '/../classes/input_stack.inc.php');

$db = new LoggerDatabase();
$stack = new InputStack($db);

$modules = array();
foreach(LoggerConfig::$modules as $name => $arModule)
{
    require_once(dirname(__FILE__) . '/..' . $arModule['src']);
    $modules[$name] = new $arModule['class']($db);
}
Parse($stack, $modules);


function Parse($stack, $modules)
{
    set_time_limit(0);
    $time = strtotime('-'.intval(LoggerConfig::$timeSessionExpires).' second');
    $time = date('Y-m-d H:i:s', $time);

    if (LoggerConfig::$cleanRefSpam == true)
    {
        echo "Detect Ref-Spam\r\n";
        $detectedSpam = $stack->DetectSpam($time);
        if (is_array($detectedSpam) && count($detectedSpam) > 0)
        {
            echo 'Detected Spam: ';
            echo implode(', ', $detectedSpam);
            echo "\r\n";
        }
    }

    echo "Start Parsing\r\n";
    $count = 0;
    $time_start = microtime(true);
    $oldestTime = false;
    for ($i = 1; $i <= 1000000; $i++)
    {
        $hits = $stack->PopHit(200, $time, $oldestTime);
        foreach($hits as $hit)
        {
            foreach($modules as $module)
            {
                $module->ParseHit($hit);
            }
            $count++;
        }
        echo $count.' ';
        if (count($hits) < 200)
            break;
    }
    $time_end = microtime(true);
    $time = $time_end - $time_start;
    echo "\r\n";
    echo $count;
    echo ' Hits, ';
    echo $time;
    echo ' sec., ';
    if ($time > 0)
        echo $count / $time;
    else
        echo 'N';
    echo " hits per sec.\r\n";

    echo "Start Purging\r\n";
    foreach($modules as $module)
    {
        $module->Purge();
        $module->Archive();
    }
    $stack->Purge($oldestTime);
}